package com.wyt.common.logger;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.spi.LoggerRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * log4j实现动态调整日志打印级别、动态设定是否打印某些类的日志
 **/
public class LoggerChange {
    private static Logger logger = LoggerFactory.getLogger(LoggerChange.class);

    /**
     * 动态设置全局或指定类的打印级别（级别为OFF表示不打印）
     * 如果不进行设置，则查询返回所有的日志类供参考使用
     */
    public String changLogger(String level, String className, String classLevel) {
        logger.info("调整全局日志打印级别{}，设置指定类打印级别{} {}", level, className, classLevel);
        org.apache.log4j.Logger rootLogger = LogManager.getRootLogger();
        String globalLevel = rootLogger.getLevel().toString();
        //设置全局日志级别
        if (StringUtils.isNotBlank(level)) {
            switch (level.toUpperCase()) {
                case "DEBUG":
                    rootLogger.setLevel(Level.DEBUG);
                    break;
                case "INFO":
                    rootLogger.setLevel(Level.INFO);
                    break;
                case "WARN":
                    rootLogger.setLevel(Level.WARN);
                    break;
                case "ERROR":
                    rootLogger.setLevel(Level.ERROR);
                    break;
                default:
                    logger.warn("暂不支持{}级别的日志设置", level);
            }
        }
        //设置指定类打印级别
        LoggerRepository loggerRepository = rootLogger.getLoggerRepository();
        if (StringUtils.isNoneBlank(className, classLevel)) {
            org.apache.log4j.Logger classLogger = loggerRepository.getLogger(className);
            switch (classLevel.toUpperCase()) {
                case "DEBUG":
                    classLogger.setLevel(Level.DEBUG);
                    break;
                case "INFO":
                    classLogger.setLevel(Level.INFO);
                    break;
                case "WARN":
                    classLogger.setLevel(Level.WARN);
                    break;
                case "ERROR":
                    classLogger.setLevel(Level.ERROR);
                    break;
                case "OFF":
                    classLogger.setLevel(Level.OFF);
                    break;
                default:
                    logger.warn("{}类暂不支持{}级别的日志设置", className, classLevel);
            }
        }
        //如果不进行全局或指定类的设置，则查询返回所有的日志类供参考使用
        Map<String, String> map = new HashMap<>();
        if (StringUtils.isAllBlank(level, className, classLevel)) {
            Enumeration loggers = loggerRepository.getCurrentLoggers();
            while (loggers.hasMoreElements()) {
                org.apache.log4j.Logger tmp = (org.apache.log4j.Logger) loggers.nextElement();
                map.put(tmp.getName(), tmp.getLevel() == null ? globalLevel : tmp.getLevel().toString());
            }
        }

        return JSON.toJSONString(map);
    }

    @GetMapping("/changLogger")
    public String changLogger2(@RequestParam(value = "globalLevel", required = false) String globalLevel,
                                  @RequestParam(value = "className", required = false) String className,
                                  @RequestParam(value = "classLevel", required = false) String classLevel) {
        String resp = changLogger(globalLevel, className, classLevel);
        return resp;
    }



}
